<?php
namespace Fyfyu\Admin\Traits;

trait ParseData
{
    /**
     * 获取 grid 所需数据
     */
    protected function getRelationData($model,$relations){
        foreach( $relations as $relation){
            $field = is_string($relation['field'])
                ? [ $relation['field'] , $this->model->{ $relation['relation'] }()->getRelatedKeyName() ]
                : false;
            $model->load([$relation['relation']=>function($query) use ($field){
                if( $field ){
                    $query->select($field);
                }
            }]);
        }
    }
    protected function getGridData($grid){
        array_push($grid['fields'],$this->model->getKeyName());
        $model = $this->model->select($grid['fields'])->paginate($this->limit);
        $this->getRelationData($model,$grid['relations']);
        return $model;
    }

    protected function getFormData($id){

    }
}
